/***************************************************************************
 *   Copyright (C) 2007 by Andreas Theofilu                                *
 *   andreas@TheoSys.at                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation version 3 of the License.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <kmessagebox.h>
#include <klocale.h>
#include <klistview.h>
#include <kdatewidget.h>
#include <knuminput.h>
#include <qcheckbox.h>
#include <qstring.h>
#include <qfile.h>
#include <qdir.h>
#include <qlistview.h>

//#include <iostream.h>

#include "ziaendwidget.h"
#include "tilgplan.h"
#include "helper.h"

ziaendWidget::ziaendWidget ( QWidget* parent, const char* name, bool modal, WFlags fl )
		: ziaendWidgetBase ( parent,name, modal,fl )
{
QString hdir = QDir::homeDirPath ();
QFile zaf;
ZIAEND Zi;
char datum[16], prz[24], rate[24], rv[2], ber[2];
int day, mon, year, fd;

	kDateWidget_Datum->setDate(QDate::currentDate());
	kListView_Table->setColumnWidth(0, 80);
	kListView_Table->setColumnWidth(1, 90);
	kListView_Table->setColumnAlignment(1, Qt::AlignRight);
	kListView_Table->setColumnWidth(2, 100);
	kListView_Table->setColumnAlignment(2, Qt::AlignRight);
	kListView_Table->setColumnWidth(3, 60);
	kListView_Table->setColumnAlignment(3, Qt::AlignCenter);
	kListView_Table->setColumnWidth(4, 60);
	kListView_Table->setColumnAlignment(4, Qt::AlignCenter);
	kListView_Table->setSortColumn(0);

	hdir.append("/.zinsaend.dat");
	zaf.setName(hdir);

	if (zaf.open(IO_ReadOnly) != FALSE)
	{
	   fd = zaf.handle();

	   while (read(fd, &Zi, sizeof(ZIAEND)) >= sizeof(ZIAEND))
	   {
	      date_int(&day, &mon, &year, Zi.Datum);
	      sprintf (datum, "%02d.%02d.%d", day, mon, year);
	      sprintf (prz, "%.3f", Zi.Zins);
	      sprintf (rate, "%.2f", Zi.NewRate);
	      rv[0] = (Zi.FixRate) ? 'F' : 'N';
	      ber[0] = (Zi.Rate) ? 'B' : 'N';
	      rv[1] = ber[1] = 0;

	      QListViewItem *element = new QListViewItem(kListView_Table, QString(datum), QString(prz), QString(rate), QString(rv), QString(ber));
	      kListView_Table->insertItem(element);
 	   }

	   zaf.close();
	}
}

ziaendWidget::~ziaendWidget()
{}

/*$SPECIALIZATION$*/
void ziaendWidget::tbSelectSlot(QListViewItem *item)
{
long datum;
int day, mon, year;
QString str;
QDate dt;

	if (!item)
	   return;

	datum = get_date((char *)item->text(0).ascii());
	date_int (&day, &mon, &year, datum);
	dt.setYMD(year, mon, day);
	kDateWidget_Datum->setDate(dt);
	kDoubleNumInput_Zinssatz->setValue(item->text(1).toDouble());
	kDoubleNumInput_Rate->setValue(item->text(2).toDouble());
	str = item->text(3);

	if (!str.compare(i18n("N")))
	   checkBox_Rate->setChecked(FALSE);
	else
	   checkBox_Rate->setChecked(TRUE);

	str = item->text(4);

	if (!str.compare(i18n("N")))
	   checkBox_KalkRate->setChecked(FALSE);
	else
	   checkBox_KalkRate->setChecked(TRUE);
}

void ziaendWidget::pbSaveSlot()
{
QString hdir = QDir::homeDirPath ();
QString str;
QFile zaf;
QListViewItem *item;
int fd, men, i;
ZIAEND Zi;

	hdir.append("/.zinsaend.dat");
	zaf.setName(hdir);

	if (zaf.open(IO_ReadWrite | IO_Truncate) == FALSE)
	{
	   KMessageBox::error(0, QString("Could not open file for reading: %1").arg(zaf.errorString()));
	   done(QDialog::Rejected);
	}
	
	fd = zaf.handle();
	// wegschreiben der Daten in der ListBox
	men = kListView_Table->childCount();
	item->firstChild();

	for (i = 0; i < men; i++)
	{
	   item = kListView_Table->itemAtIndex(i);
	   Zi.Datum = get_date ((char *)item->text(0).ascii());
	   Zi.Zins = item->text(1).toDouble();
	   Zi.NewRate = item->text(2).toDouble();
	   Zi.FixRate = (!strcmp(item->text(3).ascii(), "N")) ? 0 : 1;
	   Zi.Rate = (!strcmp(item->text(4).ascii(), "N")) ? 0 : 1;

	   write (fd, &Zi, sizeof(ZIAEND));
	}

	zaf.close ();
	done(QDialog::Accepted);
}

void ziaendWidget::pbRemoveSlot()
{
QListViewItem *item = kListView_Table->selectedItem();
long datum;
int day, mon, year;
QString str;
QDate dt;

	datum = get_date((char *)item->text(0).ascii());
	date_int (&day, &mon, &year, datum);
	dt.setYMD(year, mon, day);
	kDateWidget_Datum->setDate(dt);
	kDoubleNumInput_Zinssatz->setValue(item->text(1).toDouble());
	kDoubleNumInput_Rate->setValue(item->text(2).toDouble());
	str = item->text(3);

	if (!str.compare(i18n("N")))
	   checkBox_Rate->setChecked(FALSE);
	else
	   checkBox_Rate->setChecked(TRUE);

	str = item->text(4);

	if (!str.compare(i18n("N")))
	   checkBox_KalkRate->setChecked(FALSE);
	else
	   checkBox_KalkRate->setChecked(TRUE);

	kListView_Table->takeItem(kListView_Table->currentItem());
}

void ziaendWidget::pbInsertSlot()
{
QString datum, prz, rate, rv, ber;
QDate dt;
double dr, dp;
char hv0[128];

	dt = kDateWidget_Datum->date();

	if (dt <= QDate::currentDate())
	{
	   KMessageBox::error(0, i18n("Das Datum muss größer als das aktuelle Datum sein!"));
	   return;
	}

	dp = kDoubleNumInput_Zinssatz->value();
	dr = kDoubleNumInput_Rate->value();

	if (dp <= 0.0 && !checkBox_Rate->isChecked())
	{
	   KMessageBox::error(0, i18n("Der Zinssatz darf nicht Null sein!"));
	   return;
	}

	if (dp < 0.0 || dp > 100.0)
	{
	   KMessageBox::error(0, i18n("Der Zinnsatz darf nicht kleiner als Null oder größer als 100 sein!"));
	   return;
	}

	if (checkBox_Rate->isChecked() && dr <= 0.0)
	{
	   KMessageBox::error(0, i18n("Die Rate muss größer als Null sein!"));
	   return;
	}
	
	datum.sprintf ("%02d.%02d.%d", dt.day(), dt.month(), dt.year());
//	datum = QString::fromUtf8(hv0);
	prz = prz.setNum (dp, 'f', 3);
	rate = rate.setNum (dr, 'f', 2);
	rv = QString::fromUtf8(checkBox_Rate->isChecked() ? "F" : "N");
	ber = QString::fromUtf8(checkBox_KalkRate->isChecked() ? "B" : "N");
	QListViewItem *element = new QListViewItem(kListView_Table, datum, prz, rate, rv, ber);
	kListView_Table->clearSelection();
	kListView_Table->setCurrentItem(kListView_Table->lastItem());
	kListView_Table->insertItem(element);
}

void ziaendWidget::pbCancelSlot()
{
	done(QDialog::Rejected);
}

void ziaendWidget::cbRateSlot()
{}

void ziaendWidget::cbKalkRateSlot()
{}

void ziaendWidget::numRateSlot()
{}

void ziaendWidget::numZinssatzSlot()
{}

void ziaendWidget::dtDatumSlot()
{}



#include "ziaendwidget.moc"

